<?php

/*
 * Edit by shengxin
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */
namespace api\admin\controller;
use cmf\controller\RestAdminBaseController;
use api\mall\model\StoreModel;
use api\mall\model\StoreBoothModel;
use api\mall\model\StoreTagMapModel;
use think\facade\Db;
use think\exception\HttpResponseException;
use think\Response;
/**
 * Description of StoreController
 *
 * @author PC
 */
class StoreController extends RestAdminBaseController{
    //put your code here
    public function index(){
        
    }
    
    public function save(){
        $post = $this->request->post();
        
        if(empty($post['video_url'])){
            $post['video_url'] = '';
        }
        $post['update_time'] = time();
        $model = new StoreModel();
        
        if(isset($post['id'])){
            $post = $this->doSave($post);
            $id = $post['id'];
            $nid = $post['nid'];
            unset($post['nid']);
            
            if($id!=$nid){
                $has = $model->where('id',$nid)->find();
                if($has){
                    $this->error('编号已经存在');
                }
            }
            Db::startTrans();
            if($id!=$nid){
                $post['id'] = $nid;
                Db::name('store_attr')      ->where('item_id',$id)->update(['item_id'=>$nid]);
                Db::name('store_attr_map')  ->where('item_id',$id)->update(['item_id'=>$nid]);
                Db::name('store_attr_value')->where('item_id',$id)->update(['item_id'=>$nid]);
                Db::name('store_log')       ->where('item_id',$id)->update(['item_id'=>$nid]);
                Db::name('store_spattr')    ->where('item_id',$id)->update(['item_id'=>$nid]);
                Db::name('store_tag_map')   ->where('item_id',$id)->update(['item_id'=>$nid]);
            }
           
            $max = $model->max('item_order');
            $order = $max+1;
            $post['item_order'] = $order;
            $model->where('id',$id)->update($post);
            Db::commit();
        }else{
            $id = $post['id'] = substr(md5('NFT'.time()),0,16);
            $post = $this->doSave($post);
            $post['create_time'] = time();
            $post['item_status'] = 2;
            $post['check_count'] = 1;
//            if($post['item_price'] = 0;
            $post['item_num'] = 0;
            $store_type = $post['store_type'];
            $booth = new StoreBoothModel();
            $bitem = $booth->where('id',$store_type)->find();
            if($bitem){
                $post['check_count'] = $bitem->check_count;
                $post['booth_type'] = $bitem->booth_type;
            }
            $model->insert($post);
            $item_id = '000000';
            $model = new StoreTagMapModel();
            $model->where('item_id',$item_id)->update(['item_id'=>$post['id']]);
        }
        
        $this->success_jump('保存成功',url('store/subindex',['type'=>$post['store_type']]));
    }
    
    protected function success_jump($msg = '', $data = '', array $header = [])
    {
        $code   = 1;
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'url' => $data,
        ];
        $type     = $this->getResponseType();
        $response = Response::create($result, $type)->header($header);
        throw new HttpResponseException($response);
    }
    
    public function status($id,$status){
        $model = new StoreModel();
        $model->where('id',$id)->update(['item_status'=>$status,'update_time'=> time()]);
        $this->success('保存成功');
    }


    /**
     * 显示指定的资源
     *
     * @param  string $id
     */
    public function read($id)
    {
        $this->success('read');
    }

    /**
     * 保存更新的资源
     *
     */
    public function update()
    {
        $post = $this->request->post();
        $id=$post['id'];

        $post = $this->doSave($post);
        $model = new StoreModel();
        $post['update_time'] = time();
        $model->where('id',$id)->update($post);
        $this->success('update');
    }

    /**
     * 删除指定资源
     *
     * @param  string $id
     */
    public function delete($id)
    {
        $model = new StoreModel();
        $model->where('id',$id)->delete();
        $this->success('删除成功');
    }
    
    private function doSave($post){
        $validate = [
            'id'=>'require|length:1,16',
            'item_name'=>'require|length:2,30',
            'item_desc'=>'length:1,1000',
//            'item_num'=>'require|integer|gt:0',
            'item_type'=>'require|length:1,7',
//            'item_images'=>'require|lenght:2,60',
            'item_price'=>'egt:0'
        ];
        $msgs = [
            'id.require'=>'商品编号不能为空',
            'id.length'=>'编号长度1-16',
            'item_type.require'=>'商品类型不能为空',
            'item_type.length'=>'商品类型长度不符合',
            'item_name.require'=>'商品名称不能为空',
            'item_name.length'=>'商品名称长度不符合',
            'item_desc.require'=>'商品描述不能为空',
            'item_name.require'=>'商品名称不能为空',
            'item_price.egt'=>'价格不能为负数'
        ];
        if(isset($post['image_names'])){
            unset($post['image_names']);
        }

        $result = $this->validate($post, $validate,$msgs);
        if($result!==true){
            $this->error($result,$post);
        }
        if(isset($post['item_icon']) && isset($post['item_icon']['item_icon'])){
            $post['item_icon'] = $post['item_icon']['item_icon'];
        }
        
        if(isset($post['item_images'])){
            $imgs = $post['item_images'];
            $nlist = [];
            foreach ($imgs as $v){
                if($v){
                    $nlist[] = $v;
                }
            }
            $content= json_encode($nlist);
            $post['item_images'] = $content;
        }
        return $post;
    }
    
    public function digital(){
        $model = new StoreModel();
        $items = $model->where('store_type',1)->select();
        $this->success('ok',$items);
    }
    
    public function set_digital(){
        $id = $this->request->post('item_id');
        $bid = $this->request->post('booth_id');
        $model = new \api\mall\model\BoothModel();
        $model->where('id',$bid)->update(['item_id'=>$id]);
        $this->success('ok');
    }
    
    public function save_digital(){
        $id = $this->request->post('item_id');
        $bid = $this->request->post('booth_id');
        $model = new \api\mall\model\BoothModel();
        $model->where('id',$bid)->update(['item_id'=>$id]);
        $this->success('ok');
    }
    
    public function fahuo(){
        $id = $this->request->route('id');
        $model = new \api\mall\model\StoreLogModel();
        $model->where('id',$id)->where('status',1)->update(['status'=>2,'desc'=>'已发货']);
        $this->success('ok',$id);
    }
    
    public function del_order(){
        $id = $this->request->route('id');
        $model = new \api\mall\model\StoreLogModel();
        $model->where('id',$id)->delete();
        $this->success('ok',$id);
    }
    
    public function save_images() {
        $id = $this->request->post('id');
        $imgs = $this->request->post('imgs');
        if(empty($imgs)){
            $imgs = [];
        }
        $imgstr = json_encode($imgs);
        $model = new StoreModel();
        $model->where('id',$id)->update(['item_images'=>$imgstr]);
        $this->success('ok');
    }
    
    public function save_logistic(){
        $orderid = $this->request->post('order_id');
        $logistics = $this->request->post('logistics');
        $model = new \api\mall\model\StoreLogModel();
        $model->where('id',$orderid)->update(['logistics'=>$logistics]);
        $this->success('修改成功');
    }
}
